// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { loadImage, readStorage } from "../utils/CommonUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    itemID: number;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    // update (dt) {}

    public updateItem(itemID: number, data: any) {
        this.itemID = itemID;
        let icon = this.node.getChildByName('im_icon');
        let label_name = this.node.getChildByName('label_name');
        let label_score = cc.find('im_bg_count/label_count', this.node);
        let im_rank = this.node.getChildByName('im_no');
        let bg_rank = cc.find('im_idx_bg', this.node);
        let label_rank = cc.find('im_idx_bg/label_count', this.node);


        if ((itemID+1) <= 3) {
            bg_rank.active = false;
            im_rank.active = true;

            cc.resources.load('image/paihang_no'+(itemID+1), cc.SpriteFrame, (err: Error, spriteFrame: cc.SpriteFrame) => {
                if (err) {
                    console.log(err);
                    return;
                }
                im_rank.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });
        } else {
            im_rank.active = false;
            bg_rank.active = true;
            label_rank.getComponent(cc.Label).string = String(itemID+1);
        }

        if (data.headIcon != null && data.headIcon.length > 11) {
            loadImage(data.headIcon, (spriteFrame) => {
                icon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });   
        }

        label_name.getComponent(cc.Label).string = data.nickName;
        label_score.getComponent(cc.Label).string = String(data.data);

    }
}
